import { type VxeGridProps } from '#/adapter/vxe-table';

function camelToSnake(str: string): string {
  return str.replaceAll(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
}

export function createGridOptions(
  queryApi: any,
  autoLoad: boolean = true,
): VxeGridProps {
  return {
    checkboxConfig: {
      highlight: true,
    },
    columns: [],
    height: 'auto',
    keepSource: true,
    pagerConfig: {},
    sortConfig: {
      trigger: 'cell',
      remote: true,
    },
    proxyConfig: {
      sort: true,
      autoLoad,
      response: {
        list: null,
        result: 'records',
        total: 'total',
      },
      ajax: {
        query: async ({ page, sorts }, formValues) => {
          const orders: Array<any> = [];
          if (sorts) {
            sorts.forEach((it) => {
              orders.push({
                column: camelToSnake(it.field),
                asc: it.order === 'asc',
              });
            });
          }
          return await queryApi({
            current: page.currentPage,
            size: page.pageSize,
            orders,
            ...formValues,
          });
        },
      },
    },
    toolbarConfig: {
      slots: {
        buttons: 'toolbar_buttons',
      },
      custom: true,
      // export: true,
      // import: true,
      refresh: true,
      zoom: true,
    },
  };
}
